<template>
  <van-list>
    <div v-if="!(taskInfoCount === 0)">
      <van-cell>
        KKScode:
        <span>{{taskInfo.KKScode}}</span>
      </van-cell>
      <van-cell>
        工作描述:
        <span>{{taskInfo.description}}</span>
      </van-cell>
      <van-cell>
        厂房位置:
        <span>{{taskInfo.location}}</span>
      </van-cell>
      <van-cell>
        截止日期:
        <span>{{taskInfo.deadLine}}</span>
      </van-cell>
      <van-cell>
        工作负责人:
        <span class="leader">{{taskInfo.leader}}</span>有误:
        <van-switch v-model="taskInfo.wrongStaff" />
      </van-cell>
      <van-cell>
        工作完成:
        <van-switch v-model="taskInfo.completed" />
      </van-cell>
      <van-cell>
        执行进展:
        <van-field type="textarea" :placeholder="taskInfo.progress" />
      </van-cell>
      <van-button type="primary" @click="submit(taskIndex,taskId)">保存</van-button>
    </div>
    <div v-else>
      <loading />
    </div>
  </van-list>
</template>
<style lang="less" scoped>
.leader {
  margin-right: 20vw;
}
.van-switch {
  font-size: 22px !important;
  top: 0.2rem;
}
</style>

<script>
import loading from "@/components/loading.vue";
import { getTaskInfo, postMarkTask } from "@/api/data";

export default {
  data() {
    return {
      taskIndex:-1,
      taskId:-1,
      taskInfo: {}
    };
  },
  computed:{
    taskInfoCount(){
      return Object.keys(this.taskInfo).length;
    }
  },
  methods: {
    getTaskInfo(id) {
      getTaskInfo(id).then(res => {
        this.taskInfo = res.data;
      });
    },
    changeList(index){
       let wrongItem = window.taskList.splice(index,1);
           window.wrongList.push(wrongItem[0]);        
    },
    submit(taskIndex,id) {
      let submitData = {
        id:this.taskId,
        wrongStaff:this.taskInfo.wrongStaff,
        completed:this.taskInfo.completed,
        progress:this.taskInfo.progress
      };
      postMarkTask(submitData).then(res => {
        if(res){
          this.changeList(taskIndex);
          this.$router.go(-1);
          this.$toast(res.msg);
        }    
      });
    }
  },
  components: {
    loading
  },
  activated() {
    console.log("activated");
    this.taskIndex =  this.$route.params.index;
    this.taskId = this.$route.params.id;
    this.getTaskInfo(this.taskId);
  },
  deactivated() {
    console.log("deactivated");
    this.taskInfo = {};
  }
};
</script>
